package com.ewei.web.crawler.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

import com.ewei.web.crawler.utils.ConnectionHandler;

public class CrawlerLogin {
	private static String loginURL = "http://www.zhihu.com/login/phone_num";
	public CrawlerLogin(){
	}
	/**
	 * 登录返回Cookies
	 */
	public Map<String,String> login(String phoneNum,String password){
		Map<String,String> cookies = new HashMap<String, String>();
		try {
			Response response = ConnectionHandler.wrapper(Jsoup.connect(loginURL),"www.zhihu.com")
			    .ignoreContentType(true)
				.data("_xsrf", "1267ec4f624372b510c48944b4b22de2")
				.data("password",password)
				.data("remember_me","true")
				.data("phone_num",phoneNum)
				.method(Method.POST)
				.execute();
			System.out.println(response.body());
			cookies = response.cookies();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return cookies;
	}
	public static void main(String[] args) {
//		CrawlerLogin login = new CrawlerLogin("https://www.zhihu.com/login/phone_num");
//		login.login("13527956557","chenhuade1");
//		Timer timer = new Timer();
//		login("13527956557", "yihua031211");
	}
}
